package com.approval.approvalapp.index.cygn.yjcn;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.IdeaBean;
import com.approval.approvalapp.bean.MoniterBean;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.ContantActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class JyyjActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.bm_tv)
    TextView bmTv;
    @Bind(R.id.bm_ll)
    LinearLayout bmLl;
    @Bind(R.id.jydx_tv)
    TextView jydxTv;
    @Bind(R.id.jydx_ll)
    LinearLayout jydxLl;
    @Bind(R.id.bt_tv)
    TextView btTv;
    @Bind(R.id.bt_ll)
    LinearLayout btLl;
    @Bind(R.id.et)
    EditText et;
    @Bind(R.id.commit)
    TextView commit;
    private List<IdeaBean> ideaBeans = new ArrayList<>();
    private List<MoniterBean> qjflList = new ArrayList<>();
    private String duixiangid;

    @Override
    public int getLayoutId() {
        return R.layout.activity_jyyj;
    }

    @Override
    public void initView() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("area", SpUtil.getString(this, SpUtil.REGION_CODE, ""));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_idea_obj", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                ideaBeans = JSON.parseArray(str, IdeaBean.class);
            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {

            }
        });

    }

    @Override
    public void initData() {
        bmTv.setText(SpUtil.getString(this,SpUtil.UNIT_NAME,""));

    }

    String btStr;

    @OnClick({R.id.back_iv, R.id.bm_ll, R.id.jydx_ll, R.id.bt_ll,R.id.commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.jydx_ll:
                if (ideaBeans == null || ideaBeans.size() == 0) {
                    return;
                }
                for (int i = 0; i < ideaBeans.size(); i++) {
                    qjflList.add(new MoniterBean(ideaBeans.get(i).getDepCode(), ideaBeans.get(i).getDepName()));
                }
                final OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = qjflList.get(options1).getName();
                        jydxTv.setText(tx);
                        duixiangid = qjflList.get(options1).getId();
                    }
                }).build();
                pvOptions.setPicker(qjflList, null, null);
                pvOptions.show();
                break;
            case R.id.bt_ll:
                ContantActivity.goInto(this, 1, "标题", btStr);
                break;
            case R.id.commit:
                if (TextUtils.isEmpty(btStr) || jydxTv.getText().toString().equals("请选择")||et.getText().toString().trim().length() == 0){
                    Toast.makeText(this, "缺少提交内容", Toast.LENGTH_SHORT).show();
                    return;
                }

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("WJTITLE",btTv.getText().toString().trim());
                jsonObject.put("WJCONTENT",et.getText().toString().trim());
                jsonObject.put("DepID",duixiangid);
                jsonObject.put("ORGCODE",SpUtil.getString(this,SpUtil.ORG_CODE,""));
                jsonObject.put("ORGNAME",SpUtil.getString(this,SpUtil.UNIT_NAME,""));
                jsonObject.put("name" , SpUtil.getString(this,
                        SpUtil.USER_RUN_NICKNAME,""));
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/submit_idea",jsonObject.toString() , new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        Toast.makeText(JyyjActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                        JyyjActivity.this.finish();
                    }

                    @Override
                    public void errowithresponse(String str) {
                        ErrorBeanUtil.getErrorMessage(str);
                    }

                    @Override
                    public void erro() {
                    ErrorBeanUtil.getErrorMessage(null);
                    }

                    @Override
                    public void finish() {

                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (data != null){
                    btStr =  data.getStringExtra(ContantActivity.returnkey);
                    btTv.setText(btStr);
                }
                break;
        }
    }
}
